package com.cqeec.myblog.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cqeec.myblog.model.Article;
import com.cqeec.myblog.service.ArticleService;
import com.cqeec.myblog.service.ArticleServiceImpl;

@WebServlet("/index.html")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = -4516687438020266168L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 在Servlet中获取所有文章，然后交给JSP页面显示出来
		// 创建文章服务类对象
		ArticleService articleService = new ArticleServiceImpl();
		try {
			// 获取文章
			List<Article> list = articleService.getAllArticle();
			// 将数据带到页面
			request.setAttribute("list", list);
			// 获取文章分类标签
			Map<String, Integer> articleTypeMap = articleService.getArticleTypeAndTotal();
			request.setAttribute("atMap", articleTypeMap);
			request.getRequestDispatcher("index.jsp").forward(request, response);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
